/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.tools.remoting {
	import com.googlecode.milib.base.MLActionClass;

	import flash.net.NetConnection;
	import flash.net.Responder;

	/**
	 * @author Marek Brun
	 */
	public class MLResponderAction extends MLActionClass {

		private var responder:Responder;
		private var connection:NetConnection;
		protected var className:String;
		protected var methodName:String;
		protected var args:Array;
		public var infoData:Object={};		public var faultObject:Object;
		public var resultObejct:*;

		public function MLResponderAction(connection:NetConnection, className:String, methodName:String, args:Array=null) {
			this.connection=connection;
			this.className=className;
			this.methodName=methodName;
			this.args=args==null ? [] : args;
			responder=new Responder(onResponderResult, onResponderFault);
			
			start();
		}
		
		override protected function doStart():Boolean {
			try{
				connection.call.apply(connection, [className+'.'+methodName, responder].concat(args));
			}catch(e:Error){
				
			}
			return true;
		}
		
		
//********************************************************************************************
//		events for MLRelayResponder
//********************************************************************************************
		// Handle a successful AMF call. This method is defined by the responder. 
		protected function onResponderResult(result:Object):void {
			//String(result);
			resultObejct=result;
			_isSuccess=true;
			finish();
		}

		// Handle an unsuccessfull AMF call. This is method is dedined by the responder. 
		private function onResponderFault(fault:Object):void {
			//String(fault.description);
			faultObject=fault;
			finish();
		}
	}
}
